package com.cskaoyan.login;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

// 登录的案例
public class LoginDemo {



    // 主方法
    public static void main(String[] args) {

//        Boolean ret = login("changfeng", "bmw");

        // select * from user where username = 'changfeng' and password = 'sss' or '1=1';
        Boolean ret = login("seggead", "sffwgsdad' or '1=1");

        if (ret) {
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }

    }


    // 登录的方法
    public static Boolean login(String username,String password) {

        try {

            // 1. 获取数据库连接
            Connection connection = JDBCUtils.getConnection();

            // 2. 获取statement对象
            Statement statement = connection.createStatement();

            // 3. 执行SQL语句
            String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";
            System.out.println("sql:" + sql );
            ResultSet resultSet = statement.executeQuery(sql);

            // 4. 获取结果集
            if (resultSet.next()) {
                JDBCUtils.closeSource(connection,statement,resultSet);
                return true;
            }
            JDBCUtils.closeSource(connection,statement,resultSet);
            return false;

        }catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }


}
